import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/baseblocs/bloc_provider.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/modle/play/entity/user_account_entity.dart';
import 'package:shopping_group/utils/network_utils.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/route_util.dart';

class MyMoneyBloc implements BlocBase {
  StreamController<Object> _initController = StreamController<Object>();
  StreamSink get initDate => _initController.sink;

  StreamController<Object> _allController = StreamController<Object>();
  StreamSink get _addAllDate => _allController.sink;
  Stream<Object> get sendAllData => _allController.stream;

  StreamController<Object> _jsController = StreamController<Object>();
  StreamSink get jsDate => _jsController.sink;

  MyMoneyBloc() {
    _initController.stream.listen(allData);
    _jsController.stream.listen(jsData);
  }

  void jsData(data) {
    _jsTask(data);
  }

  void allData(data) {
    _allTask(data);
  }

  void _jsTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext).requestFocus(FocusNode());
    Map<String, dynamic> data = {};
    if (ObjectUtil.isEmptyString(BaseConstant.userId)) {
      return;
    }
    data['val'] = BaseConstant.userId;
    BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.JIESUO_URL, data: data, headers: {}, success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            BotToast.showText(text: '解锁成功');
            eventBus.fire(JiesuoOkEvent());
          }
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  void _allTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext).requestFocus(FocusNode());
    Map<String, dynamic> data = {};
    if (ObjectUtil.isEmptyString(BaseConstant.userId)) {
      return;
    }
    data['val'] = BaseConstant.userId;
    //BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.ACCOUNT_URL, data: data, headers: {}, success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            UserAccountEntity entity = UserAccountEntity.fromJson(result);
            BaseConstant.capitalAccount = entity.capitalAccount;
            BaseConstant.integralAccount = entity.integralAccount;
            BaseConstant.allAccount = entity.allAccount;
            BaseConstant.dsAllAcct = entity.dsAllAcct;
            BaseConstant.reBuyQuanAcct = entity.reBuyQuanAcct;
            BaseConstant.fuliAcct = entity.fuliAcct;
            BaseConstant.nlqAcct = entity.nlqAcct;
            BaseConstant.jfAcct = entity.jfAcct;
            BaseConstant.starLevel = entity.starLevel;
            BaseConstant.shopStar = entity.shopStar;
            BaseConstant.jinTieAcct = entity.jinTieAcct;
            BaseConstant.hadFreeGm = entity.hadFreeGm;
            BaseConstant.hadFreeGmName = entity.hadFreeGmName ?? "";
            BaseConstant.starLevelName = entity.starLevelName ?? "";
            BaseConstant.shopStarName = entity.shopStarName ?? "";
            _addAllDate.add(entity);
            eventBus.fire(BlackSjEvent());
            eventBus.fire(BlackEvent());
          }
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  @override
  void dispose() {
    _initController?.close();
    _allController?.close();
    _jsController?.close();
  }
}
